 
*This file excecutes shift-share robustness following Borusyak and Hull (2024). Table  D.18.

************************
*Municipal level industrialization
************************

*Use local level file created in muni_analysis.do. 
use "$path\elinkeino_forregs.dta" , clear
 
gen mun39=code
 
preserve 

*Counterfactual shocks
use  "$temp\counterfactual_bartik.dta", clear

egen bartik_mean=rowmean(bartik_1-bartik_999)

egen std_bartik_mean=std(bartik_mean)

forvalues i=1(1)999 {
egen bartik_std_`i'=std(bartik_`i')	
}
 

tempfile share
save `share'
restore 

joinby mun39 using  `share'

forvalues i=1(1)999 {
gen bc_`i'=	bartik_`i'-bartik_mean
gen bsdrc_`i'=	bartik_std_`i'-std_bartik_mean
gen spp_`i'= bc_`i'*post
gen tpp_`i'=bsdrc_`i'*post
 }
 
 
forvalues i=1(1)999 {
gen r_`i'=(bartik_std_`i'-std_bartik_mean)*post
 }

gen bartik_recenter=bartik_st-std_bartik_mean
gen bartik_recenter_norm=bartik2-bartik_mean
 
gen bartik_recenter_post=bartik_recenter*post
gen bartik_recenter_norm_post=bartik_recenter_norm*post
 
 
gen bartik_mean_post=bartik_mean*post
global controls  year#post year#post c.manushare_a1940#post c.agrishare_a1940#post
  
*ci 
ri_ci agrishare_a bartik_recenter_post  [aw=wei],  zsim(r_1-r_999) range(-4 2)  controls(bartik_mean_post  year#town year#city c.manushare_a1940#year c.agrishare_a1940#year) absorb(year#laani50 code)
return list
local ri_left=r(ci_left)
local ri_right=r(ci_right)

*ols
reghdfe agrishare_a bartik_recenter_post bartik_mean_post  [aw=wei], a(code year#laani50 year#town year#city c.manushare_a1940#year c.agrishare_a1940#year) cl(code)
estimates store agri_muni
estadd scalar ri_left=`ri_left'
estadd scalar ri_right=`ri_right'
 
 
labe var bartik_recenter_post "Exposure x Post"


************************
*individual level
************************

global municontrols   agrishare_1940 manushare_1940 town  city 

use "$temp\oldgen_50_temp", replace
 
reghdfe primary50_individual bartik_standardized   $municontrols  , a(fem byear laani50) cl(muni39_g)


preserve 

use  "$temp\counterfactual_bartik.dta", clear

egen bartik_mean=rowmean(bartik_1-bartik_999)

egen std_bartik_mean=std(bartik_mean)

forvalues i=1(1)999 {
egen bartik_std_`i'=std(bartik_`i')	
}
 
tempfile share
save `share'
restore 

destring mun39, replace
joinby mun39 using  `share'

forvalues i=1(1)999 {
gen bs_`i'=	bartik_`i'-bartik_mean
gen r_`i'=	bartik_std_`i'-std_bartik_mean
}
 
gen bartik_recenter=bartik_standardized-std_bartik_mean
gen bartik_recenter_norm=bartik-bartik_mean
   
*ci agri 
ri_ci primary50_individual bartik_recenter,  zsim(r_*) range(-1 2)  controls(agrishare_1940   town city manushare_1940 bartik_mean) absorb(laani50 fem byear)
*return list
local ri_left=r(ci_left)
local ri_right=r(ci_right)

   
*ols
reghdfe primary50_individual bartik_recenter  bartik_mean $municontrols, a(fem byear laani50    ) cl(muni39_g)
estimates store agri
estadd scalar ri_left=`ri_left'
estadd scalar ri_right=`ri_right'

labe var bartik_recenter "Exposure"
  

************************
 *Individual income
************************

global municontrols   agrishare_1940 manushare_1940 town  city 

use "$temp\oldgen_70_temp", replace

reghdfe ln_svatv bartik_standardized    $municontrols   , a(fem byear laani50) cl(muni39_g)

preserve 

use  "$temp\counterfactual_bartik.dta", clear

egen bartik_mean=rowmean(bartik_1-bartik_999)

egen std_bartik_mean=std(bartik_mean)

forvalues i=1(1)999 {
egen bartik_std_`i'=std(bartik_`i')	
}
 

tempfile share
save `share'
restore 

destring mun39, replace
joinby mun39 using  `share'

forvalues i=1(1)999 {
gen bs_`i'=	bartik_`i'-bartik_mean
gen r_`i'=	bartik_std_`i'-std_bartik_mean
 }
 
gen bartik_recenter=bartik_standardized-std_bartik_mean
gen bartik_recenter_norm=bartik-bartik_mean
  
*income 
ri_ci ln_svatv bartik_recenter,  zsim(r_*) range(-1 2)  controls(agrishare_1940   town city manushare_1940 bartik_mean) absorb(laani50 fem byear)
*return list
local ri_left=r(ci_left)
local ri_right=r(ci_right)

    
*ols
reghdfe ln_svatv bartik_recenter  bartik_mean $municontrols, a(fem byear laani50    ) cl(muni39_g)
estimates store income
estadd scalar ri_left=`ri_left'
estadd scalar ri_right=`ri_right'

labe var bartik_recenter "Exposure"
  
************************
*Higher degree
************************

global municontrols_cohort c.agrishare_1940#age44 c.manushare_1940#age44  city#age44 town#age44 
global municontrols   agrishare_1940 manushare_1940 town  city 


use "$temp\old_young_temp.dta", clear


reghdfe hdegree treat_young , a(age44 age44#laani50 fem $municontrols_cohort muni_g ) cl(muni_g)

preserve 

use  "$temp\counterfactual_bartik.dta", clear

egen bartik_mean=rowmean(bartik_1-bartik_999)

egen std_bartik_mean=std(bartik_mean)

forvalues i=1(1)999 {
egen bartik_std_`i'=std(bartik_`i')	
}
 

tempfile share
save `share'
restore 

destring mun39, replace
joinby mun39 using  `share'


forvalues i=1(1)999 {
gen bc_`i'=	bartik_`i'-bartik_mean
gen bsdrc_`i'=	bartik_std_`i'-std_bartik_mean
gen cp_`i'=	bc_`i'*(age44<=25)
gen r_`i'=	 bsdrc_`i'*(age44<=25)
}
 
gen bartik_recenter=bartik_standardized-std_bartik_mean
gen bartik_recenter_norm=bartik-bartik_mean
gen bartik_recenter_young=bartik_recenter*(age44<=25)
gen bartik_recenter_norm_young=bartik_recenter_norm*(age44<=25)

gen bartik_mean_young=bartik_mean*(age44<=25)

gen young2=(age44<=25)
 
global municontrols_cohort2 c.agrishare_1940#young2 c.manushare_1940#young2  city#young2 town#young2 
 

*ci
ri_ci hdegree bartik_recenter_young,  zsim(r_*) range(-1 2)  controls($municontrols_cohort   bartik_mean_young) absorb(fem age44#laani50 muni_g)
return list
local ri_left=r(ci_left)
local ri_right=r(ci_right)

*ols
reghdfe hdegree bartik_recenter_young  bartik_mean_young  , a(age44 age44#laani50 fem $municontrols_cohort muni_g ) cl(muni_g)
estimates store hde
estadd scalar ri_left=`ri_left'
estadd scalar ri_right=`ri_right'

labe var bartik_recenter_young "Exposure x Young"
 
********************
*Upward mobility
******************** 
 
use "$temp\old_young_temp.dta", clear

drop _merge

count
joinby JNRO50 using  "$temp\father_link_1950_UM", unmatched(master)
count
drop _merge


*Clean some strange observations
duplicates drop
drop if age50_head<age50
drop if age50_head<25

replace hdegree=1 if hdegree2==1
keep if young==1

tempfile um um_sample
save `um'
gen weis=1
keep if no_edu_head==1
collapse (sum) weis, by(mun39)
save `um_sample'
use `um', clear
joinby mun39 using `um_sample'

drop if weis<2

*income rank

destring mun39, replace

preserve 

use  "$temp\counterfactual_bartik.dta", clear

egen bartik_mean=rowmean(bartik_1-bartik_999)
egen std_bartik_mean=std(bartik_mean)

forvalues i=1(1)999 {
egen bartik_std_`i'=std(bartik_`i')	
}
 
tempfile share
save `share'
restore 

joinby mun39 using  `share'

forvalues i=1(1)999 {
gen bc_`i'=	bartik_`i'-bartik_mean
gen r_`i'=	bartik_std_`i'-std_bartik_mean
}
 
gen bartik_recenter=bartik_standardized-std_bartik_mean
gen bartik_recenter_norm=bartik-bartik_mean

 
keep if no_edu_head==1  

global sims bsdrc_*

ri_ci income_rank bartik_recenter,  zsim(r_*) range(-5 5)  controls(agrishare_1940  town city manushare_1940 bartik_mean) absorb(fem age50 laani50)
return list
local ri_left=r(ci_left)
local ri_right=r(ci_right)


reghdfe income_rank bartik_recenter  std_bartik_mean   $municontrols if no_edu_head==1, a(fem age50 laani50) cl(muni_g) 
estimates store income_rank
estadd scalar ri_left=`ri_left'
estadd scalar ri_right=`ri_right'

labe var bartik_recenter "Exposure"

labe var bartik_mean "Counterfactual Exposure"


esttab agri_muni agri income hde income_rank ///
using "$temp\tables\BH_exo_indi_results.tex", ///
mgroups( "Agriculture Share"  "Agriculture 1950" "Ln income"  "Higher degree" "Income Rank",   pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cmidrule(lr){@span}) ) ///
replace keep(bartik_recenter_post bartik_recenter bartik_recenter_young) nonotes ///s
b(%9.3f) se(%9.3f)  stats(N ri_left ri_right, fmt(0 3) layout(@ @ @) ///
labels("N"  "Left 95% CI" "Right 95% CI" )) label star(* .1 ** .05 *** .01) nodepvars nomtitle




 
 